package com.ithjh.controller;

import com.ithjh.domain.Address;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    //普通参数
    @RequestMapping("/save")
    @ResponseBody
    public String anyway(String name,int age){
        System.out.println("普通参数传递 name ==>" + name);
        System.out.println("普通参数传递 age ==>" + age);

        System.out.println("user anyway...");
        return "{'module','user anyway'}";
    }

    //普通参数不同名:使用@RequestParam注解
    @RequestMapping("/differentparamname")
    @ResponseBody
    public String different(@RequestParam("name") String username, int age){
        System.out.println("普通参数传递 name ==>" + username);
        System.out.println("普通参数传递 age ==>" + age);
        return "ddd";
    }

    //POJO参数
    @RequestMapping("/pojoparam")
    @ResponseBody
    public String pojoparam(Address address){
        System.out.println("pojo参数传递 address ==> " + address);
        return "ddd";
    }

    //POJO嵌套类型参数
    @RequestMapping("/pojocontainparam")
    @ResponseBody
    public String pojocontainparam(Address address){
        System.out.println("pojo嵌套类型参数传递 address ==> " + address);
        return "ddd";
    }

    //数组参数
    @RequestMapping("/arrayparam")
    @ResponseBody
    public String arraynparam(String hobbies[]){
        System.out.println("数组参数传递 hobbies ==> " + Arrays.toString(hobbies));
        return "ddd";
    }

    //集合参数
    @RequestMapping("/listparam")
    @ResponseBody
    public String listparam(@RequestParam List<String> likes){
        System.out.println("pojo嵌套类型参数传递 likes ==> " + likes);
        return "ddd";
    }

    //集合参数:json形式
    @RequestMapping("/listparamforjson")
    @ResponseBody
    public String listparamforjson(@RequestBody List<String> likes){
        System.out.println("pojo(json)参数传递 list ==> " + likes);
        return "ddd";
    }

    //日期参数
    @RequestMapping("/dataparam")
    @ResponseBody
    public String dataparam(Date date,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date1,
                            @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date date2
    ){
        System.out.println("date参数传递 date ==> " + date);
        System.out.println("date参数传递 date1(yyyy-MM-dd) ==> " + date1);
        System.out.println("date参数传递 date1(yyyy/MM/dd HH:mm:ss) ==> " + date2);
        return "ddd";
    }
}
